using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Ria.Data;

namespace ExpenseReport
{
    /// <summary>
    /// Enumeration representing the possible statuses of expense reports.
    /// </summary>
    public enum ReportStatus
    {
        NotSubmitted = 0,
        Submitted = 1,
        Approved = 2
    }

    public partial class ExpenseReport
    {
        /// <summary>
        /// EntityFramework currently doesn't support enum values so this maps the integer type for status
        /// into the enumerated values.
        /// </summary>
        [Shared]
        [Bindable(true)]
        [Display(ShortName = "Status", Description = "Expense Report Status.")]
        public ReportStatus RptStatus
        {
            get
            {
                return (ReportStatus)this.Status;
            }
            set
            {
                this.Status = (int)value;
            }
        }

    }

    [Shared]
    public static class ExpenseReportValidation
    {
        /// <summary>
        /// Validation method that checks to make sure creation date isn't greater than today's date.
        /// </summary>
        /// <param name="property">The property value being validated.</param>
        /// <param name="context">The ValidationContext representing the state of the object and property 
        /// when it is being validated.</param>
        /// <param name="validationResult">ValidationResult of the validation method including error messages.</param>
        /// <returns>Boolean indicating whether validation was successful.</returns>
        public static bool ValidateDateIncurred(object property, ValidationContext context, out ValidationResult validationResult)
        {
            validationResult = null;
            bool result = DateTime.Compare((DateTime)property, DateTime.Now) < 0;

            if (!result)
                validationResult = new ValidationResult(context.DisplayName + " must be today or in the past.");
            return result;
        }

        /// <summary>
        /// Validation method that checks to ensure there are descriptions entered for expenses greater than $1000.
        /// </summary>
        /// <param name="property">The property value being validated.</param>
        /// <param name="context">The ValidationContext representing the state of the object and property 
        /// when it is being validated.</param>
        /// <param name="validationResult">ValidationResult of the validation method including error messages.</param>
        /// <returns>Boolean indicating whether validation was successful.</returns>
        public static bool ValidateDescription(object property, ValidationContext context, out ValidationResult validationResult)
        {
            validationResult = null;
            ExpenseReportDetails er = (ExpenseReportDetails)property;
            bool result;
            if (er.Amount > 1000 && (String.IsNullOrEmpty(er.LongDescription) || String.IsNullOrEmpty(er.ShortDescription)))
            {
                result = false;
            }
            else
            {
                result = true;
            }

            if (!result)
                validationResult = new ValidationResult("For expenses greater than $1000, summary and description must " +
                                                        "be completed.");
            return result;
        }
    }
}